package com.smartwebx.ha.web.util.valve;

import static com.smartwebx.ha.web.admin.common.HaConstant.HA_USER_SESSION_KEY;

import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.service.pipeline.PipelineContext;
import com.alibaba.citrus.turbine.pipeline.valve.AbstractInputOutputValve;
import com.smartwebx.ha.biz.assign.HaStationAreaManager;
import com.smartwebx.ha.dal.config.StatGroupThreadLocal;
import com.smartwebx.ha.web.admin.common.HaUserSys;

/**
 * 为当前线程存储用户站群数据
 * 
 * @author zhangxi
 * 
 */
public class UseStatiGroupValve extends AbstractInputOutputValve {
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private HaStationAreaManager haStationAreaService;

	@Override
	public void invoke(PipelineContext pipelineContext) throws Exception {
		if (StatGroupThreadLocal.isAreaStatGroupEmpty()) { // 如果没有内容，则存入数据
			StatGroupThreadLocal.setAreaStatGroupMap(haStationAreaService
					.getAreaStarGroupMap());
		}

		HttpSession session = request.getSession();
		HaUserSys haUserSys = (HaUserSys) session
				.getAttribute(HA_USER_SESSION_KEY);

		if (haUserSys.isLogin()) {// 只有在登陆之后才会放入用户站群数据
			Integer areaId = haUserSys.getAreaId();
			Set<String> useStatAreas = StatGroupThreadLocal
					.getAreaStatGroupByAreaId(areaId);
			StatGroupThreadLocal.setInWeb(true);
			StatGroupThreadLocal.setStatGroups(useStatAreas);
		}
		pipelineContext.invokeNext();
	}
}